//
//  MKUITestImagesPageControl.m
//  MKUITestApp
//
//  Created by 苏尚进 on 2022/10/14.
//  Copyright © 2022 moon. All rights reserved.
//

#import "MKUITestImagesPageControl.h"
#import "Menu.h"
@interface MKUITestPageImagesIndicatorCollectionViewModel : MKUIPageIndicatorCollectionViewModel
@property(nonatomic,strong,nullable) UIImage *currentPageIndicatorTintImage;
@property(nonatomic,strong,nullable) UIImage *pageIndicatorTintImage;
@end
@implementation MKUITestPageImagesIndicatorCollectionViewModel
@end

@interface MKUITestImagesPageIndicatorCollectionView : MKUIDotPageIndicatorCollectionView
@end
@implementation MKUITestImagesPageIndicatorCollectionView
- (id)initWithFrame:(CGRect)frame{
    if(self=[super initWithFrame:frame]){
    }
    return self;
}
- (void)customReloadCellModel{
    [super customReloadCellModel];
    MKUITestPageImagesIndicatorCollectionViewModel *pageControlCellModel = (MKUITestPageImagesIndicatorCollectionViewModel *)self.pageControlCellModel;
    UIImage *image = nil;
    if(pageControlCellModel.selected){
        image = pageControlCellModel.currentPageIndicatorTintImage;
    }else{
        image = pageControlCellModel.pageIndicatorTintImage;
    }
    self.pageImageView.image = image;
    self.pageImageView.hidden = image==nil;
    self.pageDotView.hidden = image!=nil;
}
+ (CGSize)sizeForPageControl:(MKUIPageControl *)pageControl pageIndex:(NSInteger)pageIndex selected:(BOOL)selected{
    MKUITestPageImagesIndicatorCollectionViewModel *pageControlCellModel = (MKUITestPageImagesIndicatorCollectionViewModel *)[pageControl createPageIndicatorCollectionViewModelForPage:pageIndex];
    UIImage *image = nil;
    if(selected){
        image = pageControlCellModel.currentPageIndicatorTintImage;
    }else{
        image = pageControlCellModel.pageIndicatorTintImage;
    }
    CGSize s = [self maxSize];
    if(image){
        CGSize maxSize = CGSizeMake(10, 10);
        if(selected){
            maxSize = CGSizeMake(30, 30);
        }
        s = [MKCGRect scaleSize:image.size aspectFitToSize:maxSize];
    }
    return s;
}
@end

@implementation MKUITestImagesPageControl
- (Class<MKUIPageIndicatorCollectionViewProtocol>)pageIndicatorCellClassForPage:(NSInteger)page{
    return [MKUITestImagesPageIndicatorCollectionView class];
}
- (Class<MKUIPageIndicatorCollectionViewProtocol>)currentPageIndicatorCellClassForPage:(NSInteger)page{
    return [MKUITestImagesPageIndicatorCollectionView class];
}
- (MKUIPageIndicatorCollectionViewModel *)createPageIndicatorCollectionViewModelForPage:(NSInteger)page{
    MKUITestPageImagesIndicatorCollectionViewModel *cm = [[MKUITestPageImagesIndicatorCollectionViewModel alloc] init];
    UIImage *image = [Menu sharedMenusWithCount:10][page%10].icon;
    cm.currentPageIndicatorTintImage = image;
    cm.pageIndicatorTintImage = [image mk_grayImage];
    return cm;
}
@end

@interface MKUITestImagesPageIndicatorCollectionView_1 : MKUIColorDotPageIndicatorCollectionView
@end
@implementation MKUITestImagesPageIndicatorCollectionView_1
+ (CGSize)sizeForPageControl:(MKUIPageControl *)pageControl pageIndex:(NSInteger)pageIndex selected:(BOOL)selected{
    UIEdgeInsets insets = [self pageContentInsets];
    if(selected){
        return CGSizeMake(20+insets.left+insets.right, 10+insets.top+insets.bottom);
    }else{
        return CGSizeMake(10+insets.left+insets.right, 5+insets.top+insets.bottom);
    }
}
+ (UIEdgeInsets)pageContentInsets{
    return UIEdgeInsetsMake(1, 2, 3, 4);
}
@end

@implementation MKUITestPageControl_1
- (Class<MKUIPageIndicatorCollectionViewProtocol>)pageIndicatorCellClassForPage:(NSInteger)page{
    return [MKUITestImagesPageIndicatorCollectionView_1 class];
}
- (Class<MKUIPageIndicatorCollectionViewProtocol>)currentPageIndicatorCellClassForPage:(NSInteger)page{
    return [MKUITestImagesPageIndicatorCollectionView_1 class];
}
@end

@interface MKUITestImagesPageIndicatorCollectionView_2 : MKUIDotPageIndicatorCollectionView
@end
@implementation MKUITestImagesPageIndicatorCollectionView_2
- (id)initWithFrame:(CGRect)frame{
    if(self=[super initWithFrame:frame]){
        self.contentView.clipsToBounds = YES;
    }
    return self;
}
- (void)customLayoutSubviews{
    [super customLayoutSubviews];
    MKUIPageIndicatorCollectionViewModel *pageControlCellModel = self.pageControlCellModel;
    MKUIPageControl *pageControl = pageControlCellModel.pageControl;
    NSInteger page = pageControlCellModel.indexInSectionModel;
    NSInteger numberOfPages = pageControl.numberOfPages;
    CGRect f1 = self.pageDotView.mk_frameSafety;
    MKCGAxis X = pageControl.scrollAxis;
    if(numberOfPages>1){
        //控制端头
        CGFloat cornerRadius = MIN(f1.size.width,f1.size.height)/2;
        if(page==0){
            MKCGRectAddLength(&f1, X, cornerRadius);
        }else if(page==numberOfPages-1){
            MKCGRectAddMin(&f1, X, -cornerRadius);
            MKCGRectAddLength(&f1, X, cornerRadius);
        }
    }
    self.pageDotView.mk_frameSafety = f1;
}
+ (CGSize)sizeForPageControl:(MKUIPageControl *)pageControl pageIndex:(NSInteger)pageIndex selected:(BOOL)selected{
    return CGSizeMake(20, 6);
}
@end

@implementation MKUITestPageControl_2
- (id)initWithFrame:(CGRect)frame{
    if(self=[super initWithFrame:frame]){
        self.interitemSpacing = 1.0/[UIScreen mainScreen].scale;
    }
    return self;
}
- (Class<MKUIPageIndicatorCollectionViewProtocol>)pageIndicatorCellClassForPage:(NSInteger)page{
    return [MKUITestImagesPageIndicatorCollectionView_2 class];
}
- (Class<MKUIPageIndicatorCollectionViewProtocol>)currentPageIndicatorCellClassForPage:(NSInteger)page{
    return [MKUITestImagesPageIndicatorCollectionView_2 class];
}
@end

@implementation MKUITestPageControl_3
- (id)initWithFrame:(CGRect)frame{
    if(self=[super initWithFrame:frame]){
        self.interitemSpacing = 0;
    }
    return self;
}
@end
